window.__skin = localStorage.getItem('__skin') || 'default';
$('body').removeClass('default').addClass(window.__skin);

// 鼠标悬停，添加class，移出时去除class
function show111(selector, className){
	$(selector).mouseover(function(){
		var me = $(this);

		if(me.data("showTimer")){
			clearTimeout(me.data("showTimer"));
			me.removeData("showTimer");
		}

		me.addClass(className);
	}).mouseout(function(){
		var me = $(this);

		if(me.data("showTimer")){
			clearTimeout(me.data("showTimer"));
		}

		var timer = setTimeout(function(){
			me.removeClass(className);
			me.removeData("showTimer");
		}, 400);
		me.data("showTimer", timer);
	});
}

show111("#dropdown", "hover");

function getSelectedFiles(){
	var selected = $("#file-table tbody .checkbx.checked");

	if(selected.length == 0){
		return null;
	}

	return selected.parents("tr");
}

// 获取当前路径
function getCurrentPath(){
	return decodeURIComponent(window.__path);
}


// 列出文件
function listFiles(data){
	if(!data || data.length == 0){
		return;
	}

	var filelist = $("#file-table tbody");

	for (var i = 0; i < data.length; i++) {
		var f = data[i];

		// <tr data-fid="3" data-type="1">
		//  <td data-field="filename">
		//      <i class="fa checkbx"></i>
		//      <div class="fileicon image"></div>
		//      <a href="">5.jpg</a></td>
		//  <td data-field="size">103.65K</td>
		//  <td data-field="time">2016-04-07 04:15:20</td>
		// </tr>

		var tr = $('<tr></tr>');
		tr.attr("data-fid", f.userfileId);
		tr.attr("data-type", f.fileType ? "1" : "0");

		//文件名单元格
		var td = $('<td data-field="filename"></td>');
		// td.append('<i class="fa checkbx"></i>');
		//文件图标
		var fi = $('<div class="fileicon"></div>');
		fi.addClass(getFileIcon(f.fileName));
		td.append(fi);
		//文件链接
		var path = getCurrentPath().replace(/\/+$/, "") + "/" + f.fileName;
		var a = $('<a></a>');
		if(!f.fileType){
			// a.attr("href", "#" + window.__hash.action + "?path=" + encodeURIComponent(path));
		}else{
			a.attr("href", "/pan/userfile/download?userfileId=" + f.userfileId);
			a.attr('target', '_blank');
		}
		a.attr("path", path);
		a.text(f.fileName);
		td.append(a);
		tr.append(td);

		//文件大小单元格
		var td2 = $('<td data-field="size"></td>');
		td2.html(f.fileType ? smartSize(f.fileSize) : "-");
		tr.append(td2);

		//文件时间单元格
		var td3 = $('<td data-field="time"></td>');
		td3.html(new Date(f.uploadTime).format());
		tr.append(td3);
		filelist.append(tr);
	}
}

// 加载文件列表
function loadFiles(path, data){
	if(!defined(path)){
		throw new Error("missing arguments!");
	}

	var url = '/pan/userfile/getAllFiles';
	path = decodeURIComponent(path);

	var lid = layer.load(1, {zIndex: -1});
	// 清空列表
	var tr = $('<tr><td style="padding-left:26px">加载中...</td></tr>');
	var table = $("#file-table tbody").empty().append(tr);

	data = data || {};
	data.sortOrder = "asc";
	data.sortColumn = "filename";

	$.ajax({
		url: url,
		type: 'get',
		data: data,
		dataType: 'json',
		timeout: 10000,
		success: function(data){
			if(data.statusCode == 200){
				window.__path = path;
				if(data.data && data.data.length > 0){
					listFiles(data.data);
					tr.remove();
				}else{
					updateFileCount(0);
					tr.find("td").html('该目录下没有任何文件!');
				}
			}else{
				tr.find("td").html('<span style="color:red">服务器错误!</span>');
			}
		},
		error: function(error){
			tr.find("td").html('<span style="color:red">加载失败!</span>');
		},
		complete: function(){
			layer.close(lid);
		}
	});
}

$(function(){
	$('#file-table th[data-sortcolumn="' + window.__sortColumn + '"]').find(".sort").addClass("active");
	if(window.__sortOrder == "desc"){
		$("#file-table th").filter('[data-sortcolumn="' + window.__sortColumn + '"]').find(".sort").addClass("desc");
	}

	// 设置文件列表最大高度为剩余的高度
	$("#file-table tbody").height(function(){
		return $(window).height() - $(this).offset().top - 30;
	});
});

// 文件列表点击
$('#file-table').on("click", 'td[data-field="filename"] a', function(e){
	return;
	e.preventDefault();
	var me = $(this);
	var tr = me.parents("tr");

	// 文件夹
	if(tr.attr("data-type") == "0"){
		loadCommonFiles(me.attr("path"));
	}else{ // 文件

	}
});

//窗口加载完成刷新数据
window.onload=function () {
	var url = window.location.href;
	var url2 = new Array();
	url2 = url.split('&&');
	var url3 = new Array();
	var url4 = new Array();
	url3 = url2[0].split('=');
	url4 = url2[1].split('=');
	var userFileId = url3[1];
	var shareId = url4[1];
	$.ajax({
		url:'/pan/findUserFile',
		type:'post',
		data:{userfileId:userFileId,shareId:shareId},
		success:function (e) {
			if(e.message=="success"){
				$('.fileName').html(e.data.file.fileName);
				var time = getTime(e.data.share.shareTime);
				if(e.data.file.fileType==true){                   //如果文件不为文件夹则修改图标
					var i = 1;
					$('.type').attr('data-type',i);
					var fileIcon = getFileIcon(e.data.file.fileName);
					$('.fileicon').addClass(fileIcon);
				}
				$('.shareTime').html(time);
				$('.fileSize').html(smartSize(e.data.file.fileSize));
				if(e.data.share.shareDay==0){
					$('#shareDay').html("永久有效");
					$('#day').html("");

				}else {
					$('#shareDay').html(e.data.share.shareDay);
				}
			}
		}
	});

	//下载分享文件
	$('#download-btn').click(function () {
		var url = "/pan/userfile/download?userfileId="+userFileId;
		window.location.href = url;
	})
};




